package com.q.tqb.base;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.q.tqb.R;
import com.q.tqb.util.StatusBarUtil;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.dialog.CustomProgressDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author gpw
 */

public abstract class BaseActivity extends AppCompatActivity {


    @BindView(R.id.iv_back)
    protected ImageView mIvBack;

    @BindView(R.id.tv_title)
    protected TextView mTvTitle;

    @BindView(R.id.iv_right)
    protected ImageView mIvRight;

    @BindView(R.id.tv_right)
    protected TextView mTvRight;


    private Unbinder unBinder;

    protected CustomProgressDialog mProgressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.color_white), 240);
        BaseApplication.getInstance().addActivity(this);
        unBinder = ButterKnife.bind(this);
        mProgressDialog = new CustomProgressDialog(this);
        init(savedInstanceState);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1) {
            getResources();
        }
        super.onConfigurationChanged(newConfig);
    }


    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unBinder != null) {
            unBinder.unbind();
        }
        OkHttpUtil.getInstance().cancel(this);
        BaseApplication.getInstance().removeActivity(this);
    }

    public void setTitle(String title) {
        mTvTitle.setText(title);
    }

    public void setTvRight(String content) {
        mTvRight.setVisibility(View.VISIBLE);
        mTvRight.setText(content);
    }


    public void showToast(String content) {
        Toast.makeText(BaseActivity.this, content, Toast.LENGTH_SHORT).show();
    }


    public abstract int getLayoutId();

    public abstract void init(Bundle savedInstanceState);


}